package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqdbSettingProecss;
import com.jy.adscm.operation.mscyb.service.JqdbSettingProecssService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-17
 */
@RestController
@RequestMapping("/adscm/operation/jqdb/jqdbSettingProecss")
public class JqdbSettingProecssController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqdbSettingProecssService jqdbSettingProecssService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqdbSettingProecss jqdbSettingProecss, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqdbSettingProecss.setIsEnable(1);
        jqdbSettingProecss.setCreator(token.getUser().getId().toString());
        jqdbSettingProecss.setCreateTime(new Date());
        Wrapper<JqdbSettingProecss> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",jqdbSettingProecss.getPid());
        wrapper.orderBy("rank",false);
        wrapper.last("LIMIT 1");
        List<JqdbSettingProecss> jqdbSettingProecsses = jqdbSettingProecssService.selectList(wrapper);
        Integer rank=1;
        if (jqdbSettingProecsses!=null && jqdbSettingProecsses.size()>0 ){
            JqdbSettingProecss jqdbSettingProecss1 = jqdbSettingProecsses.get(0);
            rank=jqdbSettingProecss1.getRank()+1;
        }
        jqdbSettingProecss.setRank(rank);
        boolean insert = jqdbSettingProecssService.insert(jqdbSettingProecss);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqdbSettingProecss jqdbSettingProecss, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqdbSettingProecss.setCreator(token.getUser().getId().toString());
        jqdbSettingProecss.setCreateTime(new Date());
        boolean insert = jqdbSettingProecssService.updateById(jqdbSettingProecss);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("selectByPid/{pid}")
    public SimpleCoreResponse selectByPid(@PathVariable String pid, HttpServletRequest request) throws AdscmSystemException {
        List<JqdbSettingProecss> jqdbSettingProecss=jqdbSettingProecssService.selectByPid(pid);
        return new SimpleCoreResponse("200","查询成功",jqdbSettingProecss);
    }
}

